import random
from outlier_detection import anormal_region_detection
# from env import  Region_info, NUM_REGION
from typing import List, Dict, Any, Set, Tuple
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

prev_outliers = None

# def random_region_point(region, roadmap):

#     boundary = Region_info[region]

#     x = random.randint(min([boundary[0][0], boundary[-1][0]]), max([boundary[0][0], boundary[-1][0]]))
#     y = random.randint(min([boundary[0][1], boundary[-1][1]]), max([boundary[0][1], boundary[-1][1]]))
#     # point = (x, y)
#     while (x, y) not in roadmap[0]:
#       x = random.randint(min([boundary[0][0], boundary[-1][0]]), max([boundary[0][0], boundary[-1][0]]))
#       y = random.randint(min([boundary[0][1], boundary[-1][1]]), max([boundary[0][1], boundary[-1][1]]))
#     return (x, y)

def random_reposition(repo_observ: Dict[str, Any], roadmap: List):
    reposition = []  # type: List[Dict[str, str]]
    # rids = set([r["driver_id" for r in repo_observ]])

    for r in repo_observ["rider_id"]:
        reposition.append(dict(rider_id = r, destination = random.choice(roadmap[0])))
        print(len(roadmap), random.choice(roadmap[0]))
    return reposition

# def useOutlier(repo_observ: Dict[str, Any], model_r, model_int, roadmap):
#     global prev_outliers
#     reposition = []  # type: List[Dict[str, str]]
#     supply_exceeds_demand = list()
#     supply_donot_meet_demand = list()
#     outliers, model_r, model_int = anormal_region_detection(repo_observ, model_r, model_int)
#     regions_data = repo_observ["data"]
#     region_infos = list()
#     #print(regions_data)
#     for i in range(NUM_REGION):
#         if outliers[-1][i] == 1:
#             region_info = dict(
#                     id=i,
#                     numIdleR=regions_data[-1][i * 4 + 1],
#                     numbusyR=regions_data[-1][i * 4],
#                     numIdleO=regions_data[-1][i * 4 + 2]
#                 )
#             region_infos.append(region_info)
#     num_rep = np.zeros(10)
#     for region in region_infos:
#         if region['numIdleR'] + region['numbusyR'] > region['numIdleO']:
#             supply_donot_meet_demand.append(region['id'])  # 供不应求
#         else:
#             supply_exceeds_demand.append(region['id']) # 供过于求

#     for r in repo_observ["rider_infos"]:
#         if r["status"] == 2 and r["work_region"] in supply_donot_meet_demand:  # 只有在空闲的时候才能进行调度
#             num_rep[r["work_region"]] += 1
#             if len(supply_exceeds_demand) > 0:
#                 # distance = [repo_observ["distance"][r["work_region"]][idx] for idx in supply_exceeds_demand]
#                 # region = supply_exceeds_demand[np.argmin(distance)] # 调度最近的区域

#                 region = random.choice(supply_exceeds_demand)
#                 reposition.append(dict(rider_id = r["id"], destination = random_region_point(region,roadmap)))
#                     # agent.route = self.random_region_point(region)
#             else:
#                 random_region = random.randint(0, NUM_REGION-1)
#                 while random_region == r["work_region"]:
#                     random_region = random.randint(0, NUM_REGION-1)
#                 reposition.append(dict(rider_id = r["id"], destination = random_region_point(random_region,roadmap)))
            
#     print("异常点：", reposition, num_rep, supply_donot_meet_demand , supply_exceeds_demand, regions_data.shape, outliers.shape, outliers[-1])
#     if any(x != 0 for x in outliers[-1]):
#         result = 1
#     else:
#         result = 0
#     return reposition, model_r, model_int, result, outliers[-1]

# 不处理重调度的异常检测
def useOutlier_without(repo_observ: Dict[str, Any], model_r, model_int):
    global prev_outliers
    outliers, model_r, model_int = anormal_region_detection(repo_observ, model_r, model_int)
    regions_data = repo_observ["data"]


    # 只返回异常检测结果和模型
    print("异常点：", regions_data.shape, outliers.shape, sum(outliers), outliers[-1])
    return outliers, model_r, model_int
